<template>
  <div class="tabs">
    <el-tag
      :key="tag.label"
      type="info"
      :effect="$route.path === tag.path ? 'dark' : 'plain'"
      v-for="(tag, index) in tabList"
      closable
      :disable-transitions="false"
      @close="handleClose(tag, index)"
      @click="change(tag, index)"
    >
      {{ tag.label }}
    </el-tag>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      inputVisible: false,
      inputValue: "",
    };
  },
  methods: {
    handleClose(tag, index) {
      let length = this.tabList.length - 1;
      this.closeTab(tag);
      // 如果关闭的标签不是当前路由的话，就不跳转
      if (tag.path !== this.$route.path) {
        return;
      }
      // 关闭最后一个标签后跳转到首页
      if (index === 0) {
        return this.$router.push("/");
      }
      // 关闭的标签是最右边的话，往左边跳转一个
      if (index === length) {
        this.$router.push(this.tabList[index - 1].path);
      } else {
        // 否则往右边跳转
        this.$router.push(this.tabList[index].path);
      }
    },
    ...mapMutations("tab", {
      closeTab: "closeTab",
    }),
    // 点击标签跳转
    change(tag) {
      this.$router.push(tag.path);
    },
  },
  computed: {
    ...mapState("tab", {
      tabList: "tabList",
    }),
  },
};
</script>
<style scoped lang="less">
.tabs {
  margin-top: -12px;
  margin-left: -9px;
  .el-tag {
    margin-right: 15px;
  }
}
</style>
